package thread;
import java.net.*;
import java.io.*;

public class TCPEchoServer {
    public final static int defaultPort=2014;
    public static void main(String[] args){
        try{
            ServerSocket ss=new ServerSocket(defaultPort);
            System.out.printf("Server dang lang nghe tai cong "+defaultPort);
            while(true){
                try{
                    Socket s = ss.accept();
                    OutputStream os = s.getOutputStream();
                    InputStream is = s.getInputStream();
                    int ch=0;
                    while(true){
                        ch=is.read();
                        System.out.print(is);
                        if(ch==-1) break;
                        os.write(ch);
                    }
                    s.close();
                }
                catch(IOException e){
                    System.err.printf(" Connection Error"+e);
                }
            }
        }
        catch(IOException e){
            System.err.printf(" Server Creation Error"+e);
        }
    }
}
